package parser;

import static statisticalanalysis.StatisticalAnalysis.getDegreeDistribution;
import static statisticalanalysis.StatisticalAnalysis.printBasicMetrics;

import java.util.Vector;

import stackexchange.User;
import stackexchange.UserGroup;
import stackexchange.analyzer.ExpertAnalyzer;
import stackexchange.analyzer.ExpertAnalyzerFactory;
import stackexchange.analyzer.evaluator.EvaluatorFactory;
import stackexchange.analyzer.evaluator.ExpertEvaluator;
import stackexchange.analyzer.evaluator.F1Score;
import stackexchange.posts.Answer;
import stackexchange.posts.Question;
import statisticalanalysis.Interval;
import statisticalanalysis.StatisticalAnalysis;
import static stackexchange.Constants.PROC_OF_USERS;

public class Research {
	private static String locOfFiles = "c:/sexch/astr/";
	private static XmlParser parser;

	private static Vector<Question> allQuestions;
	private static Vector<Answer> allAnswers;
	private static Vector<User> allUsers;
	private static int nDisplayedUsers = 15;

	public static void gainResult(String analyzerName, String evaluatorName,
			boolean showExperts, boolean showTrueExperts) {
		/**
		 * analyze
		 */
		ExpertAnalyzer analyzer = new ExpertAnalyzerFactory()
				.getExpertAnalyzer(analyzerName);
		UserGroup analyzedExperts = analyzer.analyze(new UserGroup(allUsers),
				allQuestions, allAnswers);
		/***/
		/**
		 * evaluate
		 */
		ExpertEvaluator evaluator = new EvaluatorFactory()
				.getEvaluator(evaluatorName);
		evaluator.setData(new UserGroup(allUsers), allQuestions, allAnswers);
		UserGroup trueExperts = evaluator.getExperts();
		/***/
		/**
		 * show analyzed and/or true experts
		 */
		if (showTrueExperts) {
			trueExperts.showUsers(nDisplayedUsers);
		}
		if (showExperts) {
			analyzedExperts.showUsers(nDisplayedUsers);
		}
		/***/
		/**
		 * show overview
		 */
		F1Score result = evaluator.getResult(analyzedExperts);
		System.out.println("-------------");
		System.out.println("Analyzed via \"" + analyzerName
				+ "\" and evaluated by \"" + evaluatorName + "\"");
		System.out.println("-------------");
		System.out.println(result);

		System.out.println("		E-------------E");
	}

	public static void main(String[] args) {
		parser = new XmlParser(locOfFiles);
		allQuestions = parser.getAllQuestions();
		allAnswers = parser.getAllAnswers();
		allUsers = parser.getAllUsers();
		// gainResult("ZScoreAnalyzer", "VBREvaluator");
		System.out.println("Analyze with "
				+ ((int) allUsers.size() * PROC_OF_USERS)
				+ " analyzed users and " + ((int) allUsers.size() * 0.02)
				+ " evaluated users");
		// gainResult("ZscoreAnalyzer", "ReputationEvaluator", false, false);
		gainResult("ReachAnalyzer", "ReputationEvaluator", false, false);
//		 gainResult("AnswerAnalyzer", "ReputationEvaluator", false, false);
		 gainResult("PRAnalyzer", "ReputationEvaluator", false, false);
		// gainResult("PRAnalyzer", "VBREvaluator");//TODO -> vbr manyak la

		// ExpertAnalyzer analyzer = new ExpertAnalyzerFactory()
		// .getExpertAnalyzer("PRAnalyzer");
		// UserGroup analyzedExperts = analyzer.analyze(new UserGroup(allUsers),
		// allQuestions, allAnswers);
		// printStatAnalysis(analyzedExperts.getUsers(), "PR");
		//
		// ExpertEvaluator evaluator = new EvaluatorFactory()
		// .getEvaluator("ReputationEvaluator");
		// evaluator.setData(new UserGroup(allUsers), allQuestions, allAnswers);
		// UserGroup rep = evaluator.getExperts();
		// Vector<Interval> dist = getDegreeDistribution(rep.getUsers(),
		// "getReputation", 15);
		// printStatAnalysis(rep.getUsers(), "REP.EVAL");
	}

	public static void printStatAnalysis(Vector<User> vector, String title) {

		printBasicMetrics(vector, "getNofComments", title + " --- COMMENTS ---");
		printBasicMetrics(vector, "getReputationWithin1Mounth", title
				+ " --- REP IN 1 MONTH ---");
		printBasicMetrics(vector, "getReputationWithin3Mounths", title
				+ " --- REP IN 3 MONTH 1");
		printBasicMetrics(vector, "getReputationWithin6Mounths", title
				+ " --- REP IN 6 MONTH 1");
		printBasicMetrics(vector, "getReputationWithin1Year", title
				+ " --- REP IN 1 Year1");
		printBasicMetrics(vector, "getReputationWithin18Mounths", title
				+ " --- REP IN 1.5 YEAR 1");
		printBasicMetrics(vector, "getReputationWithin2Years", title
				+ " --- REP IN 2 YEARS 1");
		printBasicMetrics(vector, "getReputation", title + " --- REPUTATION 1");
		printBasicMetrics(vector, "getProfileViews", title + " --- VIEWS 2");
		printBasicMetrics(vector, "getUpVotes", title + " --- UP 3");
		printBasicMetrics(vector, "getDownVotes", title + " --- DOWN 4");
		printBasicMetrics(vector, "getNofQuestions", title + " --- #Q 5");
		printBasicMetrics(vector, "getNofAnswers", title + " --- #A 6");
		printBasicMetrics(vector, "getTotalQuestionLength", title
				+ " --- TOTAL Question Length 7");
		printBasicMetrics(vector, "getAverageQuestionLength", title
				+ " --- Average Question Length 8");
		printBasicMetrics(vector, "getTotalAnswerLength", title
				+ " --- TOTAL. Answer Length 9");
		printBasicMetrics(vector, "getAverageAnswerLength", title
				+ " --- Average Answer Length 10");
		printBasicMetrics(vector, "getNofBadges", title + " --- N of Badges 10");
		printBasicMetrics(vector, "getNofEdits", title + " --- N of EDTS 10");
	}

}
